package cn.vastbase.jdbc.type;

import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

import oracle.jdbc.OracleTypes;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

public class StructTestCase {
    public static void main(String[] args) {
        try {
            String driver = "cn.com.vastbase.Driver";
            String jdbcUrl = "jdbc:vastbase://172.16.107.129:11110/ctp2?prepareThreshold=0";
            String user = "admin";
            String pwd = "admin";
            Connection conn = DriverManager.getConnection(jdbcUrl, user, pwd);
            conn.setAutoCommit(false);
            System.out.println("连接成功");
            String query_settleItemCfg = "SELECT * FROM t_settleitemcfg WHERE ROWNUM = 1";
            PreparedStatement pstmt = conn.prepareStatement(query_settleItemCfg);
            ResultSet rs = pstmt.executeQuery();
            List<Object> list = new ArrayList<>();
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();
            if (rs.next()) {
                for (int i = 0; i < columnCount; i++) {
                    list.add(rs.getObject(i + 1));
                }
            }
            System.out.println("TY_SETTLEITEMCFG入参属性个数："+list.size());
            Struct tySettleitemcfg1 = conn.createStruct("TY_SETTLEITEMCFG", list.toArray(new Object[0]));

            String query_page = "select StartIndex,EndIndex,OrderColumns,OrderDirection from t_page where rownum = 1";
            Struct page = null;
            pstmt = conn.prepareStatement(query_page);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                page = conn.createStruct("TY_PAGE", new Object[]{
                        rs.getInt("StartIndex"),
                        rs.getInt("EndIndex"),
                        rs.getString("OrderColumns"),
                        rs.getString("OrderDirection")
                });
            }
            if (page == null) {
                System.out.println("No data found in t_page table.");
                return;
            } else {
                System.out.println("data in t_page table.");
            }

            CallableStatement cstmt = conn.prepareCall("{call pkgi_settlemgrt.up_checksettlemanager(?, ?, ?, ?, ?, ?, ?, ?)}");
            cstmt.setObject(1, tySettleitemcfg1);
            cstmt.setObject(2, page);
            cstmt.registerOutParameter(3, OracleTypes.STRUCT, "TY_SETTLEITEMCHECK");
            cstmt.setString(4, "0000.admin");
            cstmt.registerOutParameter(5, OracleTypes.NUMBER); // 页码
            cstmt.registerOutParameter(6, OracleTypes.CURSOR); // 游标
            cstmt.registerOutParameter(7, OracleTypes.NUMBER); // 返回码
            cstmt.registerOutParameter(8, OracleTypes.VARCHAR);
            cstmt.execute();
            Struct settleItemCheck = (Struct) cstmt.getObject(3);
            Object[] attributes1 = settleItemCheck.getAttributes();
            System.out.println("TY_SETTLEITEMCFG出参属性个数："+attributes1.length);
            System.out.println(Arrays.toString(attributes1));
        } catch (SQLException e) {
            System.out.println("执行失败");
            e.printStackTrace();
        }

    }
}
